﻿namespace ImageP
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.IO;
    using System.Windows.Forms;
    using System.Text;

    public partial class Form1 : Form
    {
        private bool opened = false;
        private ReadImage RI = new ReadImage();
        private Enhancements enhance = new Enhancements();
        private RGBcolor[,] mybuffer;
        private Bitmap bm;
        private Rotate rotateIm = new Rotate();
        private string filePath, fileExtension;
        private Histogram histogram = new Histogram();
        private int drawn = 0;
        private int[] colors = new int[256];
        private int G = 0, R = 0, B = 0, All = 0;
        private bool sel;
        private Rectangle selection;
        private bool matching = false;
        private OperationsForm frm;

        public Form1()
        {
            InitializeComponent();
        }

        public void pic(Image im)
        {
            pictureBox1.Refresh();
            pictureBox1.Image = im;
        }

        public void ClearHistogram()
        {
            chart1.Series["allColors"].Points.Clear();
            chart1.Series["red"].Points.Clear();
            chart1.Series["green"].Points.Clear();
        }

        public void drawHistogram()
        {
            if (this.drawn == 0)
            {
                this.ClearHistogram();
                this.colors = this.histogram.DrawHistogram("All colors", pictureBox1.Image.Height, pictureBox1.Image.Width, pictureBox1.Image);
            }

            if (checkBox1.Checked == true)
            {
                this.colors = this.histogram.DrawHistogram("All colors", pictureBox1.Image.Height, pictureBox1.Image.Width, pictureBox1.Image);
            }

            if (checkBox2.Checked == true && this.R == 0)
            {
                this.colors = this.histogram.DrawHistogram("Red", pictureBox1.Image.Height, pictureBox1.Image.Width, pictureBox1.Image);
            }

            if (checkBox3.Checked == true && this.G == 0)
            {
                this.colors = this.histogram.DrawHistogram("Green", pictureBox1.Image.Height, pictureBox1.Image.Width, pictureBox1.Image);
            }

            if (checkBox4.Checked == true && this.B == 0)
            {
                this.colors = this.histogram.DrawHistogram("Blue", pictureBox1.Image.Height, pictureBox1.Image.Width, pictureBox1.Image);
            }

            for (int i = 0; i < 256; i++)
            {
                if (checkBox1.Checked == true || this.drawn == 0)
                {
                    chart1.Series["allColors"].Points.AddXY(i, this.colors[i]);
                }
                else
                {
                    chart1.Series["allColors"].Points.Clear();
                }

                if (checkBox2.Checked == true && this.R == 0)
                {
                    chart1.Series["red"].Points.AddXY(i, this.colors[i]);
                }

                if (checkBox3.Checked == true && this.G == 0)
                {
                    chart1.Series["green"].Points.AddXY(i, this.colors[i]);
                }

                if (checkBox4.Checked == true && this.B == 0)
                {
                    chart1.Series["blue"].Points.AddXY(i, this.colors[i]);
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "*.JPG|*.JPG|*.GIF|*.GIF|*.PPM|*.PPM|(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.* ";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                int index = saveFileDialog1.FilterIndex;
               //// SaveFormats save = new SaveFormats();
               // RI.checkFormat(index, pictureBox1.Image, saveFileDialog1.FileName);
                ////save.checkFormat(index, pictureBox1.Image, saveFileDialog1.FileName);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = this.rotateIm.rotateImage(pictureBox1.Image, "left");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = this.rotateIm.rotateImage(pictureBox1.Image, "right");
        }

        private void openImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.drawn == 1)
            {
                this.drawn = 0;
            }

            this.opened = true;
            OpenFileDialog getFile = new OpenFileDialog();
            getFile.ShowDialog();
            this.filePath = getFile.FileName;
            this.RI.FilePath = this.filePath;
            this.fileExtension = Path.GetExtension(this.filePath);
            
            ////textBox1.Text = filePath;
           
            if (this.fileExtension == ".jpg" || this.fileExtension == ".png" || this.fileExtension == ".gif" || this.fileExtension == ".bmp")
            {
                this.RI.LoadCommanFormats();
                Image im = Image.FromFile(this.filePath);
                pictureBox1.Width = im.Width;
                pictureBox1.Height = im.Height;
                pictureBox1.Image = im;
                if (this.drawn == 0)
                {
                    this.drawHistogram();
                    this.drawn = 1;
                }
            }
            else
            {
                this.RI.LoadOtherFormats();
                this.bm = new Bitmap(this.RI.ImageWidth, this.RI.ImageHeight);
                pictureBox1.Width = this.RI.ImageWidth;
                pictureBox1.Height = this.RI.ImageHeight;
                this.bm = this.RI.OpenOtherFormats();
                pictureBox1.Image = this.bm;
                if (this.drawn == 0)
                {
                    this.drawHistogram();
                    this.drawn = 1;
                }
            }
        }

        private void resizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void Form1_Load(object sender, EventArgs e)
        {     
        }

        private void equalizationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.mybuffer = new RGBcolor[this.RI.ImageHeight, this.RI.ImageWidth];
            this.mybuffer = this.enhance.Equalize(this.RI.Buffer, this.RI.ImageWidth, this.RI.ImageHeight);
            this.RI.Buffer = this.mybuffer;

            this.show_image(this.RI, this.pictureBox2);   
        }

        private void show_image(ReadImage obj, PictureBox bx)
        {
            this.bm = new Bitmap(obj.ImageWidth, obj.ImageHeight);
            this.bm = obj.OpenOtherFormats();
            bx.Width = obj.ImageWidth;
            bx.Height = obj.ImageHeight;

            bx.Image = this.bm;
        }

        private void quantizationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.drawn == 1)
            {
                this.drawn = 0;
            }
            Bitmap m = new Bitmap(this.RI.ImageWidth, this.RI.ImageHeight);
            m = this.RI.Quantization(2, this.filePath);
            pictureBox1.Image = m;
            if (this.drawn == 0)
            {
                this.drawHistogram();
                this.drawn = 1;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "*.JPG|*.JPG|*.GIF|*.GIF|*.PPM|*.PPM|(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.* ";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                int index = saveFileDialog1.FilterIndex;
                SaveFormats save = new SaveFormats();
                save.CheckFormat(index, pictureBox1.Image, saveFileDialog1.FileName, this.RI.ImgType, this.RI.Comment, this.RI.Flag);
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.sel = true;
                this.selection = new Rectangle(new Point(e.X, e.Y), new Size());
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.sel)
            {
                this.selection.Width = e.X - this.selection.X;
                this.selection.Height = e.Y - this.selection.Y;
                pictureBox1.Refresh();
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (this.sel)
            {
                Pen pen = Pens.GreenYellow;
                e.Graphics.DrawRectangle(pen, this.selection);
            }
        }

        private Image Crop(Image image, Rectangle selection)
        {
            Bitmap bmp = image as Bitmap;
            Bitmap cropBmp = bmp.Clone(selection, bmp.PixelFormat);
            image.Dispose();

            return cropBmp;
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.drawn == 1)
            {
                this.drawn = 0;
            }

            Bitmap bmp = pictureBox1.Image as Bitmap;
            Image img = this.Crop(pictureBox1.Image, this.selection);
            pictureBox1.Image = img;
            if (this.drawn == 0)
            {
                this.drawHistogram();
                this.drawn = 1;
            }
        }

        private void checkBox3_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.G == 0)
            {
                this.drawHistogram();
                this.G = 1;
            }
            else if (this.G == 1)
            {
                this.G = 0;
                chart1.Series["green"].Points.Clear();
            }
        }

        private void checkBox2_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.R == 0)
            {
                this.drawHistogram();
                this.R = 1;
            }
            else if (this.R == 1)
            {
                this.R = 0;
                chart1.Series["red"].Points.Clear();
            }
        }

        private void checkBox4_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.B == 0)
            {
                this.drawHistogram();
                this.B = 1;
            }
            else if (this.B == 1)
            {
                this.B = 0;
                chart1.Series["blue"].Points.Clear();
            }
        }

        private void checkBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.All == 0)
            {
                this.drawHistogram();
                this.All = 1;
            }
            else if (this.All == 1)
            {
                this.All = 0;
                chart1.Series["allColors"].Points.Clear();
            }
        }

        private void calculationsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.frm = new OperationsForm(pictureBox1.Image, this.RI.Buffer, this.RI.ImageWidth, this.RI.ImageHeight, this.opened, this.matching);
            this.frm.Show();
        }

        private void grayScaleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            contrast con = new contrast(pictureBox1.Image, this.RI);
            con.Show();
        }

        private void laToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Filters f = new Filters();
            Bitmap bm = new Bitmap(pictureBox1.Image);

            bm = f.LaplaceSharpening(pictureBox1.Image);
            this.pictureBox2.Image = bm;
        }


        private void horizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Filters fs = new Filters();
            Bitmap bm;
            bm = fs.HorizontalEdgeDetection(this.pictureBox1.Image);
            this.pictureBox2.Image = bm;
        }

        private void verticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Filters fs = new Filters();
            Bitmap bm;
            bm = fs.VerticalEdgeDetection(this.pictureBox1.Image);
            this.pictureBox2.Image = bm;
        }

        private void rightDiagonalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Filters fs = new Filters();
            Bitmap bm;
            bm = fs.DiagonalRightEdgeDetection(this.pictureBox1.Image);
            this.pictureBox2.Image = bm;
        }

        private void leftDiagonalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Filters fs = new Filters();
            Bitmap bm;
            bm = fs.DiagonalLeftEdgeDetection(this.pictureBox1.Image);
            this.pictureBox2.Image = bm;
        }

        private void laPlaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Filters fs = new Filters();
            Bitmap bm;
            bm = fs.LaPlaceEdgeDetection(pictureBox1.Image);
            this.pictureBox2.Image = bm;
        }

        private void horizontalToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Filters fs = new Filters();
            Bitmap bm;
            bm = fs.HorizontalSharpening(this.pictureBox1.Image);
            this.pictureBox2.Image = bm;
        }

        private void verticalToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Filters fs = new Filters();
            Bitmap bm;
            bm = fs.VerticalSharpening(this.pictureBox1.Image);
            this.pictureBox2.Image = bm;
        }

        private void diagonalRightToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Filters fs = new Filters();
            Bitmap bm;
            bm = fs.DiagonalRightSharpening(this.pictureBox1.Image);
            this.pictureBox2.Image = bm;
        }

        private void diagonalLeftToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Filters fs = new Filters();
            Bitmap bm;
            bm = fs.DiagonalLeftSharpening(this.pictureBox1.Image);
            this.pictureBox2.Image = bm;
        }

        private void gaussianFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.button1.Visible = true;
            this.textBox1.Visible = true;
            this.label1.Visible = false;
            this.button2.Visible = false;
            this.label2.Visible = true;
        }

        private void meanFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.button2.Visible = true;
            this.textBox1.Visible = true;
            this.label2.Visible = false;
            this.button1.Visible = false;
            this.label1.Visible = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Filters fs = new Filters();
            Bitmap bm;
            int sigma = Convert.ToInt32(textBox1.Text);
            bm = fs.GaussianFilter(this.pictureBox1.Image, sigma);
            this.pictureBox2.Image = bm;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Filters fs = new Filters();
            Bitmap bm;
            int maskSize = Convert.ToInt32(this.textBox1.Text);
            bm = fs.MeanFilter(this.pictureBox1.Image, maskSize);
            this.pictureBox2.Image = bm;
        }

        private void histogramMatchingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.matching = true;
            this.frm = new OperationsForm(this.pictureBox1.Image, this.RI.Buffer, this.RI.ImageWidth, this.RI.ImageHeight, this.opened, this.matching);
            this.frm.Show();
        }
    }
}
